package com.meixiang.view.address;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.account.AddressEntity;
import com.meixiang.tool.Tool;

/**
 * 选择地址
 * author: LZRUI
 */
public class WheelViewAddressDialog extends Dialog {

    public static final String TAG = "WheelViewAddressDialog";

    private IResultListener listener;
    private TextView mTitleTextView;
    private TextView right_title;
    private String title;
    private String rightTitle;
    private Context context;
    private View view;
    private CityPickerLayout cityPickerLayout;

    /**
     * 无右边title按钮构造
     *
     * @param context  上下文对象
     * @param title    标题
     * @param listener 回调
     */
    public WheelViewAddressDialog(Context context, String title, IResultListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.title = title;
        show();
    }

    /**
     * 有右边title按钮构造
     *
     * @param context    上下文对象
     * @param title      标题
     * @param rightTitle 右边标题
     * @param listener   回调
     */
    public WheelViewAddressDialog(Context context, String title, String rightTitle, IResultListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.title = title;
        this.rightTitle = rightTitle;
        show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_wheel_addrss, null);
        setContentView(view);
        initDialog();
        initView();
    }

    private void initDialog() {
        setCancelable(false);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        int width = Tool.getWindowWith(context);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = width;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    private void initView() {
        cityPickerLayout = (CityPickerLayout) findViewById(R.id.city_picker);
        mTitleTextView = (TextView) findViewById(R.id.title);
        right_title = (TextView) findViewById(R.id.right_title);
        mTitleTextView.setText(title);
        right_title.setText(rightTitle);
        right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.RightTitleOnclick(cityPickerLayout.getProvince(), cityPickerLayout.getCity(), cityPickerLayout.getDis());
                dismiss();
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onStart() {
        WheelViewAddressDialog.super.onStart();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }

    public interface IResultListener {
        /**
         * 右title点击事件
         */
        void RightTitleOnclick(AddressEntity pro, AddressEntity city, AddressEntity dis);
    }
}
